home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Very Best of Atari Inside
/
The Very Best of Atari Inside 1.iso
/
mint
/
mntlib43
/
mntlib
/
isatty.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-09-22
|
2KB
|
82 lines
/* from the original GCC TOS library by jrd */
/* this algorithm is due to Allan Pratt @ Atari. Thanks Allan! */
#include <errno.h>
#include <osbind.h>
#include <fcntl.h>
#include <stdio.h>
#include <ioctl.h>
#include <unistd.h>
#include <string.h>
#include <support.h>
#include <stat.h>
#include <mintbind.h>
#include "lib.h"
struct __open_file __open_stat[__NHANDLES];
int isatty(fd)
int fd;
{
int rc;
long oldloc;
int handle = __OPEN_INDEX(fd);
if (handle < __NHANDLES)
if (__open_stat[handle].status != FH_UNKNOWN)
return(__open_stat[handle].status == FH_ISATTY);
oldloc = Fseek(0L, fd, SEEK_CUR); /* save current location */
if (Fseek(1L, fd, SEEK_CUR) != 0) { /* try to seek ahead one byte */
/* got either a file position or an error (usually EBADARG indicating
a range error from trying to seek past EOF), so it is not a tty */
rc = 0;
(void) Fseek(oldloc, fd, SEEK_SET); /* seek back to original location */
}
else
rc = 1; /* yes, tty */
if (handle < __NHANDLES)
if (rc) {
__open_stat[handle].status = FH_ISATTY;
__open_stat[handle].flags = CRMOD|ECHO;
}
else
__open_stat[handle].status = FH_ISAFILE;
return (rc); /* return true, false, or error */
}
/* _isctty(): determine if a file descriptor refers to this process's
controlling tty.
*/
int
_isctty(fd)
int fd;
{
#if 0
char ctty_name[L_ctermid];
char ftty_name[L_ctermid];
#endif
struct stat st, tt;
extern int __mint;
if (!(isatty(fd)) || !(isatty(-1)))
return 0;
if (fd == -1)
return 1;
#if 1
if (__mint >= 9 && !Fcntl (fd, &st, FSTAT) && !Fcntl (-1, &tt, FSTAT)) {
/* shouldn't this be as good? the stuff below takes ages...
(still loses on /dev/aux etc but ttyname can't be much better)
*/
return (st.st_dev == tt.st_dev && st.st_ino == tt.st_ino);
}
/* We know that __mint < 9 (the Fcntl's above don't have the chance
to fail), use the same algorithm that ttyname() uses in this
case: it returns "/dev/aux" if fd == -2 */
return fd != -2;
#else
(void) ctermid(ctty_name);
(void) strncpy(ftty_name, ttyname(fd), L_ctermid);
return !(strncmp(ctty_name, ftty_name, L_ctermid));
#endif
}